package com.example.alarmcontrolpanel;


import java.util.Calendar;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;


public class AlramRecord
{
	int alarm_id;
	String desc;
	int[] repeatDay;
	int repeatType;
	Calendar Cal;
	Context contextApp;
	
	int AlarmType = AlarmManager.RTC_WAKEUP;
	AlarmManager alarms;
	Intent alarmReceiver;
	public AlramRecord(int id, String description, int[] repeatDays, int repeatTypes,Calendar calendar)
	{
		alarm_id=id;
		desc=description;
		repeatDay=repeatDays;
		repeatType = repeatTypes;
		Cal = Calendar.getInstance();
		Cal.setTimeInMillis(calendar.getTimeInMillis());
	}
	public AlramRecord(String description, int[] repeatDays, int repeatTypes,Calendar calendar)
	{
		desc=description;
		repeatDay=repeatDays;
		repeatType = repeatTypes;
		Cal = Calendar.getInstance();
		Cal.setTimeInMillis(calendar.getTimeInMillis());
		contextApp = Main.CONTEXT_APP;
	}
	/**This code is only for one time. when the alarm is created for the 1st time.
	 * and has nothing to do with the alarm repeating mechanism*/
	public void setAlarm()
	{
		Calendar localCal = Calendar.getInstance();
		/**
		 * desc - description for the alarm
		 * repeatDay - Days to repeat
		 * createTime - Time of creation of Alarm
		 * createDate - Date of creation of Alarm
		 * repeatType - Type of repeat
			 * 0 - repeat Days
			 * 1 - repeat None
			 * 2 - repeat Monthly
			 * 3 - repeat Yearly
		 */
		
		switch(this.repeatType)
		{
			case 0://repeat Days
			{
				int i;
				int j=localCal.get(Calendar.DAY_OF_WEEK)-1;
				
				
				int check=0;
				/**
				 * This code checks if the user has selected any repeating day or not.
				 * if yes then it will be repeated on that day else
				 * will be triggered when the clock ticks the time next.
				 * 
				 */
				for(i=0;i<7;i++)
				{
					if(this.repeatDay[i]==1)
					{
						check=1;
						break;
					}
				}
				if(check==0)
				{
					if(this.Cal.getTimeInMillis()<localCal.getTimeInMillis())
					{
						this.Cal.add(Calendar.DATE, 1);
					}
				}
				else
				{
					check=0;
					int counter=0;
					i=j;
					//Ab hua tension
					for(int m=0;m<7;m++)					// This is to check for the next coming repeat Day
					{			
						if(i==6)
						{
							if(this.Cal.getTimeInMillis()<localCal.getTimeInMillis())
							{
								i=0;
							}
							else
							{
								if(this.repeatDay[i]==1)
									break;
								else
									i=0;
							}
						}									//
						if(this.repeatDay[i]==1)			//
							break;							//
						else
							i++;
						counter++;
					}
					this.Cal.add(Calendar.DATE, counter);
				}
				break;
			}
			case 1:
			{
				if(this.Cal.getTimeInMillis()<localCal.getTimeInMillis())
				{
					this.Cal.add(Calendar.MONTH, 1);
				}
				break;
			}
			case 2:
			{
				//repeat Monthly
				this.Cal.set(Calendar.MONTH, localCal.get(Calendar.MONTH));
				if(this.Cal.compareTo(localCal)<=0)//Cal after localCal
				{
					this.Cal.add(Calendar.MONTH, 1);
				}
				break;
			}
			case 3:
			{
				//repeat Yearly
				this.Cal.set(Calendar.YEAR, localCal.get(Calendar.YEAR));
				if(this.Cal.compareTo(localCal)<=0)// Cal after localCal
				{
					this.Cal.add(Calendar.YEAR, 1);
				}
				break;
			}
		}
		/**
		 * Here we will call the counter as a serial number used in any database.
		 * and the current value of counter will be given to the alarm_id and the value of
		 * counter will be incremented and stored in the file.
		 * */
		SharedPreferences sharedPrefCounter = contextApp.getSharedPreferences("com.Counter", 0);
		this.alarm_id=sharedPrefCounter.getInt("Count", 0);
		sharedPrefCounter.edit().putInt("Count", this.alarm_id+1).apply();
		/**
		 * The Value of the counter fetched and incremented and again stored in the file.
		 * */
		SharedPreferences sharedAlarmList = contextApp.getSharedPreferences("com.AlarmList", 0);
		sharedAlarmList.edit().putString("Key"+Integer.toString(this.alarm_id), prepareAlarmStatement()).apply();
		
		/**
		 * The ALamr is stored in the database as a file and then later will be retreived
		 * using file handling and string operations.
		 * */
		
		alarmReceiver = new Intent(this.contextApp, AlarmReceiver.class);
		alarmReceiver.putExtra("ALARM_ID", Integer.toString(this.alarm_id));
		alarms = (AlarmManager) this.contextApp.getSystemService(Context.ALARM_SERVICE);
		PendingIntent PendingAlarm = PendingIntent.getBroadcast(this.contextApp, this.alarm_id, alarmReceiver, PendingIntent.FLAG_CANCEL_CURRENT);
		//In the above code alarm_id works  as an unique Identifier for the alarm.
		alarms.set(AlarmManager.RTC_WAKEUP, this.Cal.getTimeInMillis(), PendingAlarm);
	}
	protected String prepareAlarmStatement()
	{
		String strDay = new String();
		for(int i=0;i<repeatDay.length;i++)
		{
			strDay += Integer.toString(repeatDay[i]);
		}
		String str;
		str=Integer.toString(this.alarm_id)+"`"+this.desc+"`"+strDay+"`"+Integer.toString(this.repeatType)+"`"+Long.toString(this.Cal.getTimeInMillis());
		return str;
	}
	public boolean isNull()
	{
		if(this.alarm_id>0)
			return true;
		else
			return false;
	}
	public AlramRecord getAlarm(int id, Context con)
	{
		this.contextApp=con;
		int alarm_id = 0;
		int[] repeatDay = new int[7];
		int repeatType = 0;
		String desc = "";
		Calendar Cal = Calendar.getInstance();
		SharedPreferences sharedAlarmList = contextApp.getSharedPreferences("com.AlarmList", 0);
		String newStr=sharedAlarmList.getString("Key"+Integer.toString(id), null);
		
		String calcStr;
		if(newStr!=null) /** This checks to confirm that there is something in the string */
		{
			for(int i=0;i<5;i++) /** From here the code goes for Sting manipulation */
			{
				if(i<4)
				{
					calcStr = newStr.substring(0,newStr.indexOf('`'));
					newStr = newStr.substring(newStr.indexOf('`')+1);
				}
				else
				{
					calcStr = newStr;
				}
				switch(i)
				{
					case 0://Alarm Id
					{
						alarm_id  = Integer.parseInt(calcStr);
						break;
					}
					case 1://Description
					{
						desc  = calcStr;
						break;
					}
					case 2://Repeat Day
					{
						for(int m=0;m<7;m++)
						{
							repeatDay[m]=Integer.parseInt(Character.toString(calcStr.charAt(m)));
						}
						break;
					}
					case 3://repeat Type
					{
						repeatType = Integer.parseInt(calcStr);
						break;
					}
					case 4://Calendar
					{
						Cal= Calendar.getInstance();
						Cal.setTimeInMillis(Long.parseLong(calcStr));
						break;
					}
				}
			}
		}
		AlramRecord alarms = new AlramRecord(alarm_id, desc, repeatDay, repeatType, Cal);
		return alarms;
	}
}
